/**
 * Created by Administrator on 2017/9/8.
 */
define(function (require) {
    function execute() {

        // jQuery.cookie库
        var $ = require("jquery");
        require("jquery.cookie")

        var leftTitleDom = document.getElementById("left-title");
        var leftContentDom = document.getElementById("left-content");

        leftTitleDom.innerHTML = '<li class="active">课堂学生</li><li>签到学生</li>';
        leftContentDom.innerHTML =
            '<li class="active"><ul class="stu-list"></ul></li>' +
            '<li><ul class="stu-list"></ul></li>';


        var allStudentTitleLi = leftTitleDom.querySelectorAll("li")[0],
            checkedStudentTitleLi = leftTitleDom.querySelectorAll("li")[1],
            allStudentContentLi = leftContentDom.querySelectorAll("li")[0],
            checkedStudentContentLi = leftContentDom.querySelectorAll("li")[1],
            allStudentList = allStudentContentLi.querySelector(".stu-list"),
            checkedStudentList = checkedStudentContentLi.querySelector(".stu-list");

        // click事件
        allStudentTitleLi.addEventListener("click", function () {
            checkedStudentContentLi.classList.remove("active");
            checkedStudentTitleLi.classList.remove("active");
            allStudentTitleLi.classList.add("active");
            allStudentContentLi.classList.add("active");
        });
        checkedStudentTitleLi.addEventListener("click", function () {
            checkedStudentTitleLi.classList.add("active");
            checkedStudentContentLi.classList.add("active");
            allStudentTitleLi.classList.remove("active");
            allStudentContentLi.classList.remove("active");
        });

        var teacherHandler = new require("userHandler").TeacherHandler();

        // 获取所有参加课堂学生
        teacherHandler.getCourseMember($.cookie("courseId"), function (data) {
            for (var i in data) {
                (function (data) {
                    var tmp = document.createElement("LI");
                    tmp.innerHTML = data.name + '<div class="fn-remove"></div>';
                    // 删除
                    tmp.getElementsByClassName("fn-remove")[0].addEventListener("click", function () {
                        teacherHandler.deleteCourseMember($.cookie("courseId"), data.username,function () {
                            allStudentList.removeChild(tmp);
                        })
                    })
                    allStudentList.appendChild(tmp)
                }(data[i]));
            }
        });

        // 获取已经签到的学生
        teacherHandler.getCheckedCourseMember($.cookie("courseId"),function (data) {
            for (var i in data){
                var tmp = document.createElement("LI");
                tmp.innerHTML = data[i].name;
                checkedStudentList.appendChild(tmp);
            }
        });

        // 更新签到学生
        var ws = require("webSocketChannel");
        ws.addEventListener("message", function (e) {
            var msg = JSON.parse(e.data);
            switch (msg.type) {
                case "new-check-in":
                    console.log("new-check-in",msg)
                    var tmp = document.createElement("LI");
                    tmp.innerHTML = msg.data;
                    checkedStudentList.appendChild(tmp);
                    break;
                case "new-check-out":
                    console.log("new-check-out");
                    break;
            }
        });
    }
    return{
        execute:execute
    }

});